package com.example.side_pull_menu.Fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.side_pull_menu.Fragmentkj7_two;
import com.example.side_pull_menu.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

public class Fragment_three extends Fragment{
    private TextView tv_word;
    private TextView tv_element;
    private EditText et_element;
    private Button bu_set_up;
    private Fragment_three mContext;
    private List<Fragmentkj7_two.DataBean> fragmentkj2s=new ArrayList<>();
    public Fragment_three() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_three, container, false);
        initView(view);
        submit();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bu_set_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences element = getContext().getSharedPreferences("element", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=element .edit();
                editor.putString("name",""+et_element.getText());
                editor.apply();
                submit();
                //Toast.makeText(getContext(),"设置成功",Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView(View view) {
        tv_word=(TextView)view.findViewById(R.id.tv_words);
        tv_element = (TextView) view.findViewById(R.id.tv_element);
        et_element = (EditText) view.findViewById(R.id.et_element);
        bu_set_up = (Button) view.findViewById(R.id.bu_set_up);
    }

    private void submit() {
        SharedPreferences element = getContext().getSharedPreferences("element", Context.MODE_PRIVATE);
        String name = element.getString("name", "");
        if (!TextUtils.isEmpty(name)) {
            tv_word.setText("当前 1-4 号小车账户余额告警阈值为"+name+"元");
        }else {
            tv_word.setText("当前 1-4 号小车账户余额告警阈值未设置");
        }
    }
    private void compare(){
        SharedPreferences element = getContext().getSharedPreferences("element", Context.MODE_PRIVATE);
        String name = element.getString("name", "");
        int name1= Integer.parseInt(name);
        if (name1<= 1000){
            Toast.makeText(getContext(),"警告低于账户余额阈值请充值！",Toast.LENGTH_SHORT).show();
        }
    }
}
